--    Author    : MAYANTING
--    Name      : ADM.RPT_SUST_DLMG_PAST_SCALE_LOAB.HQL
--    Functions : 表20：贷款余额分企业规模统计表
--    Purpose   :
--    Revisions or Comments
--    VER        DATE        AUTHOR           DESCRIPTION
--   ---------  ----------  ---------------  ------------------------------------
--    1.0        2018-06-11  MAYANTING           1.CREATE THE PROCEDURE
--
INSERT OVERWRITE TABLE ADM.RPT_SUST_DLMG_PAST_SCALE_LOAB PARTITION (DATA_DATE = '20180331')
SELECT  AREA.AREA_CODE_4              AS FIN_ORG_DIST
       ,T.CCY                         AS CCY
       ,T.ENTERPRISE_SCALE            AS ENTERPRISE_SCALE
       ,T.SCALE_DESC                  AS SCALE_DESC
       ,SUM(T.ACTUAL_BAL)/100000000   AS ACTUAL_BAL      -- 贷款余额
       ,SUM(T.BAL_LM    )/100000000   AS BAL_LM          -- 余额上期末
       ,SUM(T.BAL_YF    )/100000000   AS BAL_YF          -- 余额年初 0101
       ,SUM(T.BAL_GROW  )/100000000   AS BAL_GROW        -- 同比增长
       ,SUM(T.WSUM_BAL  )/100000000   AS WSUM_BAL        -- 余额加权值
       ,SUM(T.WBAL_LM   )/100000000   AS WBAL_LM         -- 余额加权值上期末
       ,SUM(T.WBAL_YF   )/100000000   AS WBAL_YF         -- 余额加权值年初
       ,SUM(T.WBAL_LC   )/100000000   AS WBAL_LC         -- 余额加权值去年同期
FROM (
    -- 大型企业 中型企业 小型企业 微型企业
    SELECT
     YE.FIN_ORG_NO                                                AS FIN_ORG_NO
    ,YE.CCY                                                       AS CCY
    ,YE.ENTERPRISE_SCALE                                          AS ENTERPRISE_SCALE    -- 企业规模
    ,CASE WHEN YE.ENTERPRISE_SCALE='CS01' THEN '大型企业'
          WHEN YE.ENTERPRISE_SCALE='CS02' THEN '中型企业'
          WHEN YE.ENTERPRISE_SCALE='CS03' THEN '小型企业'
          WHEN YE.ENTERPRISE_SCALE='CS04' THEN '微型企业'
    END                                                           AS SCALE_DESC         -- 企业规模描述
    ,COALESCE(YE.ACTUAL_BAL,0)                                    AS ACTUAL_BAL         -- 贷款余额
    ,COALESCE(YE.BAL_LM,0)                                        AS BAL_LM             -- 余额上期末
    ,COALESCE(YE.BAL_YF,0)                                        AS BAL_YF             -- 余额年初 0101
    ,COALESCE(YE.ACTUAL_BAL,0) - COALESCE(YE.BAL_LC,0)            AS BAL_GROW           -- 同比增长
    ,COALESCE(YE.WSUM_BAL,0)                                      AS WSUM_BAL           -- 余额加权值
    ,COALESCE(YE.WBAL_LM,0)                                       AS WBAL_LM            -- 余额加权值上期末
    ,COALESCE(YE.WBAL_YF,0)                                       AS WBAL_YF            -- 余额加权值年初
    ,COALESCE(YE.WBAL_LC,0)                                       AS WBAL_LC            -- 余额加权值去年同期
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE ENTERPRISE_SCALE IN('CS01','CS02','CS03','CS04') AND DATA_DATE='20180331') YE

    UNION ALL
    -- 合计
    SELECT
     YE.FIN_ORG_NO                                                AS FIN_ORG_NO
    ,YE.CCY                                                       AS CCY
    ,'Z'                                                          AS ENTERPRISE_SCALE     -- 企业规模
    ,'合计'                                                       AS SCALE_DESC     -- 企业规模描述
    ,COALESCE(YE.ACTUAL_BAL,0)                                    AS ACTUAL_BAL         -- 贷款余额
    ,COALESCE(YE.BAL_LM,0)                                        AS BAL_LM             -- 余额上期末
    ,COALESCE(YE.BAL_YF,0)                                        AS BAL_YF             -- 余额年初 0101
    ,COALESCE(YE.ACTUAL_BAL,0) - COALESCE(YE.BAL_LC,0)            AS BAL_GROW           -- 同比增长
    ,COALESCE(YE.WSUM_BAL,0)                                      AS WSUM_BAL           -- 余额加权值
    ,COALESCE(YE.WBAL_LM,0)                                       AS WBAL_LM            -- 余额加权值上期末
    ,COALESCE(YE.WBAL_YF,0)                                       AS WBAL_YF            -- 余额加权值年初
    ,COALESCE(YE.WBAL_LC,0)                                       AS WBAL_LC            -- 余额加权值去年同期
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE ENTERPRISE_SCALE IN('CS01','CS02','CS03','CS04') AND DATA_DATE='20180331') YE

    UNION ALL

    -- 中小企业 'CS02','CS03','CS04'
    SELECT
     YE.FIN_ORG_NO                                                AS FIN_ORG_NO
    ,YE.CCY                                                       AS CCY
    ,'CS234'                                                      AS ENTERPRISE_SCALE   -- 企业规模
    ,'中小企业'                                                   AS SCALE_DESC         -- 企业规模描述
    ,COALESCE(YE.ACTUAL_BAL,0)                                    AS ACTUAL_BAL         -- 贷款余额
    ,COALESCE(YE.BAL_LM,0)                                        AS BAL_LM             -- 余额上期末
    ,COALESCE(YE.BAL_YF,0)                                        AS BAL_YF             -- 余额年初 0101
    ,COALESCE(YE.ACTUAL_BAL,0) - COALESCE(YE.BAL_LC,0)            AS BAL_GROW           -- 同比增长
    ,COALESCE(YE.WSUM_BAL,0)                                      AS WSUM_BAL           -- 余额加权值
    ,COALESCE(YE.WBAL_LM,0)                                       AS WBAL_LM            -- 余额加权值上期末
    ,COALESCE(YE.WBAL_YF,0)                                       AS WBAL_YF            -- 余额加权值年初
    ,COALESCE(YE.WBAL_LC,0)                                       AS WBAL_LC            -- 余额加权值去年同期
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE ENTERPRISE_SCALE IN('CS02','CS03','CS04') AND DATA_DATE='20180331') YE

    UNION ALL

    -- 小微企业 'CS03','CS04'
    SELECT
     YE.FIN_ORG_NO                                                AS FIN_ORG_NO
    ,YE.CCY                                                       AS CCY
    ,'CS34'                                                       AS ENTERPRISE_SCALE  -- 企业规模
    ,'小微企业'                                                   AS SCALE_DESC        -- 企业规模描述
    ,COALESCE(YE.ACTUAL_BAL,0)                                    AS ACTUAL_BAL         -- 贷款余额
    ,COALESCE(YE.BAL_LM,0)                                        AS BAL_LM             -- 余额上期末
    ,COALESCE(YE.BAL_YF,0)                                        AS BAL_YF             -- 余额年初 0101
    ,COALESCE(YE.ACTUAL_BAL,0) - COALESCE(YE.BAL_LC,0)            AS BAL_GROW           -- 同比增长
    ,COALESCE(YE.WSUM_BAL,0)                                      AS WSUM_BAL           -- 余额加权值
    ,COALESCE(YE.WBAL_LM,0)                                       AS WBAL_LM            -- 余额加权值上期末
    ,COALESCE(YE.WBAL_YF,0)                                       AS WBAL_YF            -- 余额加权值年初
    ,COALESCE(YE.WBAL_LC,0)                                       AS WBAL_LC            -- 余额加权值去年同期
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE ENTERPRISE_SCALE IN('CS03','CS04') AND DATA_DATE='20180331') YE
  )T
 -- 与地区表最细级关联
 LEFT JOIN (SELECT * FROM DIMENSION.T_ORG_AREA_RELATION WHERE AREA_LEVEL='3') REAL ON T.FIN_ORG_NO=REAL.ORG_ID AND '20180331' BETWEEN REAL.START_DATE AND REAL.END_DATE
 LEFT JOIN DIMENSION.T_AREA_CODE AREA ON REAL.AREA_CODE=AREA.AREA_CODE_4 AND '20180331' BETWEEN AREA.START_DATE AND AREA.END_DATE
 GROUP BY  AREA.AREA_CODE_4
          ,T.CCY
          ,T.ENTERPRISE_SCALE
          ,T.SCALE_DESC
;